//package com.ocom.gateway.config;
//
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.core.io.buffer.DataBufferUtils;
//import org.springframework.http.HttpHeaders;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
///**
// * @Description: TODO
// * @Author: 朱晖
// * @Date: 2023/3/4
// **/
//
//@Component
//public class CacheBodyGatewayFilter implements Ordered, GlobalFilter {
//
//	@Override
//	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//		HttpHeaders headers = exchange.getRequest().getHeaders();
//		return DataBufferUtils.join(exchange.getRequest().getBody())
//				.flatMap(dataBuffer -> {
//					byte[] bytes = new byte[dataBuffer.readableByteCount()];
//					dataBuffer.read(bytes);                  // 释放堆外内存
//					DataBufferUtils.release(dataBuffer);
//					ServerHttpRequest mutatedRequest = new ServerHttpRequestDecorator(exchange.getRequest()) {
//						@Override
//						public Flux<DataBuffer> getBody() {
//							return Flux.defer(() -> {
//								DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);
//								DataBufferUtils.retain(buffer);
//								return Mono.just(buffer);
//							});
//						}
//
//						@Override
//						public HttpHeaders getHeaders() {
//							return headers;
//						}
//					};
//					return chain.filter(exchange.mutate().request(mutatedRequest).build());
//				});
//	}
//
//	@Override
//	public int getOrder() {
//		return 2;
//	}
//}
